package com.start.common.dataImport;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson2.JSONObject;
import com.start.common.dic.DictMapperParams;
import com.start.common.dic.StartBaseDicMapperHandler;
import com.start.common.exceptions.StartBusException;
import org.apache.poi.ss.usermodel.Sheet;

import java.io.BufferedInputStream;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * excel 导入
 */
public class StartExcelImport extends StartBaseExcelActionData implements ExcelReadDataListener {

    private final DictMapperParams dictMapperParams;
    private final File file;
    private ExcelReadDataListener listener;

    public void setListener(ExcelReadDataListener listener) {
        this.listener = listener;
    }

    public StartExcelImport(DictMapperParams dictMapperParams, String filePath) {
        this(dictMapperParams, new File(filePath));
    }

    public StartExcelImport(DictMapperParams dictMapperParams, File file) {
        this.dictMapperParams = dictMapperParams;
        this.file = file;
        if (dictMapperParams == null) {
            throw new StartBusException("excel 映射参数不能为空");
        }
        if (!file.exists()) {
            throw new StartBusException("文件不存在:" + file.getAbsolutePath());
        }
        if (!file.isFile()) {
            throw new StartBusException("该文件路径是一个目录");
        }
    }

    @Override
    public void onStart() {
        if (listener != null) {
            listener.onStart();
        }
    }

    @Override
    public boolean onError() {
        if (listener != null) {
            listener.onError();
        }
        return false;
    }

    @Override
    public void onEnd() {
        if (listener != null) {
            listener.onEnd();
        }
    }

    @Override
    public void onReadRow(JSONObject row) {
        if (listener == null) {
            return;
        }
        listener.onReadRow(row);
    }

    @Override
    public void start() {
        BufferedInputStream inputStream = FileUtil.getInputStream(file);
        ExcelReader excelReader = ExcelUtil.getReader(inputStream);
        excelReader.setHeaderAlias(dictMapperParams.getHeaderAlias());
        excelReader.setIgnoreEmptyRow(true);
        Sheet sheet = excelReader.getSheet();
        int firstRowNum = sheet.getFirstRowNum();
        int lastRowNum = sheet.getLastRowNum();
        List<Object> headerList = excelReader.readRow(firstRowNum);
        Map<Integer, StartBaseDicMapperHandler> handlerMap = new HashMap<>();
        for (int i = 0; i < headerList.size(); i++) {
            if (!dictMapperParams.getHeaderAlias().containsKey(headerList.get(i).toString())) {
                continue;
            }
            String fieldName = dictMapperParams.getHeaderAlias().get(headerList.get(i).toString());
            StartBaseDicMapperHandler handler = dictMapperParams.getHandlerFiled().getOrDefault(fieldName, null);
            if (handler == null) continue;
            if (handlerMap.containsKey(i)) {
                continue;
            }
            handlerMap.put(i, handler);
        }
        Set<Integer> columnIndex = handlerMap.keySet();
        for (int i = 1; i <= lastRowNum; i++) {
            List<Object> objects = excelReader.readRow(i);
            JSONObject row = new JSONObject();
            for (Integer index : columnIndex) {
                if (index >= objects.size()) {
                    continue;
                }
                StartBaseDicMapperHandler handler = handlerMap.get(index);
                row.put(handler.getFieldName(), handler.getImportValue(objects.get(index).toString()));
            }
            onReadRow(row);
        }
        this.onEnd();
    }
}
